package com.mjh.service.impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mjh.bean.Clue;
import com.mjh.bean.Customer;
import com.mjh.listener.DemoDataListener;
import com.mjh.mapper.ClueMapper;
import com.mjh.mapper.CustomerMapper;
import com.mjh.model.BaseQuery;
import com.mjh.service.ClueService;
import com.mjh.utils.AuthUtil;
import com.mjh.vo.ClueVo;
import com.mjh.vo.PieData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Service
public class ClueServiceImpl implements ClueService {
    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Override
    public PageInfo<ClueVo> queryClueListPage(Integer current, Integer size) {
        PageHelper.startPage(current, size);
        List<ClueVo> pageInfo =  clueMapper.selectList(new BaseQuery());
        return new PageInfo<>(pageInfo);
    }

    @Override
    public Boolean saveClue(Clue clue) {
        clue.setOwnerId(AuthUtil.getLoginUserId());
        clue.setCreateBy(AuthUtil.getLoginUserId());
        clue.setCreateTime(new Date());
        return clueMapper.insertSelective(clue)>0;
    }

    @Override
    public Clue queryClueInfoById(Integer clueId) {
        return clueMapper.selectByPrimaryKey(Long.valueOf(clueId));
    }

    @Override
    public Boolean modifyClue(Clue clue) {
        clue.setEditBy(AuthUtil.getLoginUserId());
        clue.setEditTime(new Date());
        return clueMapper.updateByPrimaryKeySelective(clue)>0;
    }

    @Override
    public void batchSaveClue(MultipartFile file) throws IOException {
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        EasyExcel.read(file.getInputStream(), Clue.class, new DemoDataListener(clueMapper)).sheet().doRead();
    }

    @Override
    public ClueVo queryClueDetailInfoById(Integer id) {
      return   clueMapper.selectClueDetailInfoById(id);
    }

    @Override
    public Boolean removeClueById(Integer id) {
        return clueMapper.deleteByPrimaryKey(Long.valueOf(id))>0;
    }

    @Override
    public Boolean removeClueByIds(List<Integer> ids) {
      return   clueMapper.batchDeleteCluesByIds(ids)==ids.size();
    }

    @Override
    public List<PieData> queryNotewayList() {
        return clueMapper.selectNotewayList();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean transformToCustomer(Integer clueId) {
        // 修改线索状态为“已转换客户”
        Clue clue = new Clue();
        clue.setId(clueId);
        clue.setState(-1);
        clue.setEditTime(new Date());
        clue.setEditBy(AuthUtil.getLoginUserId());
        int i = clueMapper.updateByPrimaryKeySelective(clue);
        if (i > 0) {
            // 根据标识查询线索信息
            Clue clueInfo = clueMapper.selectByPrimaryKey(Long.valueOf(clueId));
            // 新增客户记录
            Customer customer = new Customer();
            customer.setClueId(clueId);
            customer.setCreateBy(AuthUtil.getLoginUserId());
            customer.setCreateTime(new Date());
            customer.setProduct(clueInfo.getIntentionProduct());
            customer.setDescription(clueInfo.getDescription());
            customerMapper.insertSelective(customer);
        }
        return i>0;
    }

    @Override
    public Set<ClueVo> queryClueNameList() {
        return clueMapper.selectNameList();
    }


}
